package com.starbaba.weeklyAwards.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import com.base.common.database.BaseDao;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.weeklyAwards.model.WeeklyAwardsModel;

@SuppressWarnings({"deprecation", "rawtypes"})
@Component
public class WeeklyAwardsService {
	
	private BaseDao dao = new BaseDao(Constant.POOL_LIFE);
	
	public void showPage(Map<String, Object> paramsMap) throws Exception{
		List<WeeklyAwardsModel> prdNameList = dao.list("select name from weekly_awards_config ", WeeklyAwardsModel.class);
		paramsMap.put("prdNameList", prdNameList);
	}

	public List<WeeklyAwardsModel> getList(Integer start, Integer length, PageResponse pageResponse, Map<String, Object> paramsMap) throws Exception {

		String id = (String) paramsMap.get("id");
		String name = (String) paramsMap.get("name");
		String status = (String) paramsMap.get("status");
		
		StringBuilder whereParams = new StringBuilder();
		if (StringUtils.isNotEmpty(id)) {
			whereParams.append("and id = '" + id + "' ");
		}
		if (StringUtils.isNotEmpty(name)) {
			whereParams.append("and name = '" + name + "' ");
		}
		if (StringUtils.isNotEmpty(status)) {
			whereParams.append("and status = '" + status + "' ");
		}
		
		StringBuilder sql_list = new StringBuilder("select * from weekly_awards_config where 1 = 1 and status <> '3' ");
		sql_list.append(whereParams).append("order by ctime desc ");
		
		String sqlCount = "select count(1) from (" + sql_list.toString() + ") b ";
		if (start != null && length != null) {
			sql_list.append("limit " + start + ", " + length);
		}
		
		int count = dao.getInt(sqlCount);
		List<WeeklyAwardsModel> list =  dao.list(sql_list.toString(), WeeklyAwardsModel.class);
		
		for (WeeklyAwardsModel model : list) {
			model.setUrl("http://lucky/" + model.getId());
		}
		
		pageResponse.setITotalRecords(count);
		pageResponse.setITotalDisplayRecords(count);
		
		return list;
		
	}

	public String deleteInfo(Map<String, Object> paramsMap) throws Exception {
		String message = "";
		String id = (String) paramsMap.get("id");
		if (StringUtils.isEmpty(id)) {
			message = "id 不存在 ！！！";
		} else {
			int count = dao.update("update weekly_awards_config set status = '3' where id = ? ", new Object[]{id});
			message = "成功删除" + count + "条记录！！！";
		}
		return message;
	}

	public void getInfo(Map<String, Object> paramsMap) throws Exception {
		String id = (String) paramsMap.get("id");
		WeeklyAwardsModel mode = dao.get("select * from weekly_awards_config where id = ? ", WeeklyAwardsModel.class, id);
		if (mode == null) {
			paramsMap.put("model", new WeeklyAwardsModel());
		} else {
			paramsMap.put("model", mode);
		}
	}

	public String editInfo(Map<String, Object> paramsMap) throws Exception {

		String id = (String) paramsMap.get("id");
		String name = (String) paramsMap.get("name");
		String image = (String) paramsMap.get("image");
		String thumbnail = (String) paramsMap.get("thumbnail");
		String begin_date = (String) paramsMap.get("begin_date");
		String end_date = (String) paramsMap.get("end_date");
		String cost_point = (String) paramsMap.get("cost_point");
		String phone = (String) paramsMap.get("phone");
		String userid = (String) paramsMap.get("userid");
		String join_count = (String) paramsMap.get("join_count");
		String init_count = (String) paramsMap.get("init_count");
		String operator = (String) paramsMap.get("operator");
		String prd_detail = (String) paramsMap.get("prd_detail");
		String campa_detail = (String) paramsMap.get("campa_detail");
		String more_info = (String) paramsMap.get("more_info");
		String off = (String) paramsMap.get("off");
		String thumbnail2 = (String) paramsMap.get("thumbnail2");
		
		// url拼接
		WeeklyAwardsModel model = new WeeklyAwardsModel(name, image, thumbnail, thumbnail2, begin_date, end_date, cost_point, 
				phone, userid, join_count, init_count, operator, prd_detail, campa_detail, more_info);
		String now = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		String message = "";
		if (StringUtils.isEmpty(id)) {
			// 新增
//			Long newId = Sequence.getSequence();
//			model.setId(newId.toString());
			model.setCtime(now);
			model.setUtime(model.getCtime()); 
			model.setStatus("1");
			dao.add(model);
			message = "新增成功！！！";
			
		} else if (StringUtils.isNotEmpty(off)) {
			// 上下线
			WeeklyAwardsModel dbModel = dao.get("select * from weekly_awards_config where id = ? ", WeeklyAwardsModel.class, id);
			if ("1".equals(dbModel.getStatus())) {
				dbModel.setStatus("0");
				dao.update(dbModel, "id");
				message = "下线成功";
			} else if ("0".equals(dbModel.getStatus())) {
				dbModel.setStatus("1");
				dao.update(dbModel, "id");
				message = "上线成功";
			}
			
		} else {
			// 更新
			WeeklyAwardsModel dbModel = dao.get("select * from weekly_awards_config where id = ? ", WeeklyAwardsModel.class, id);
			model.setUtime(now);
			model.setId(dbModel.getId());
			dao.update(model, "id");
			message = "更新成功！！！";
		}
		return message;
	}
	
}
